<?php

/**
 * Rooms Model for RoomBooking Component
 * 
 * @version    1.0
 * @package    Joomla
 * @subpackage RoomBooking
 * @copyright (C) 2011 Giovanni Marzulli
 * @license	   GNU/GPL
 */
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport('joomla.application.component.model');

class RoomBookingModelStatistics extends JModel {

    /**
     * Rooms data array
     *
     * @var array
     */
    var $_data;

    /**
     * Returns the query
     * @return string The query to be used to retrieve the rows from the database
     */
    function _buildQuery() {
        $id_user = JRequest::getVar('id_user');
        if ($id_user == null)
            $id_user = 0;
        $month = JRequest::getVar('month');
        if ($month == null)
            $month = 0;
        $year = JRequest::getVar('year');
        if ($year == null)
            $year = 0;

        $where = '';
        if ($id_user != 0) {
            $where = ' where ';
            $id_user_str = ' id_user = ' . $id_user . ' ';
        }else
            $id_user_str = '';
        if ($year != 0) {
            $where = ' where ';
            if ($id_user != 0)
                $year_str = ' AND year = ' . $year . ' ';
            else
                $year_str = ' year = ' . $year . ' ';
        }else
            $year_str = '';
        if ($month != 0) {
            $where = ' where ';
            if ($id_user != 0 || $year != 0)
                $month_str = ' AND month = ' . $month . ' ';
            else
                $month_str = ' month = ' . $month . ' ';
        }else
            $month_str = '';

        $query = 'select c.id,c.id_user,c.year,c.month,SUM(c.cost) as cost,SUM(c.charge) as charge from #__roombooking_costs AS c ' . $where . ' ' . $id_user_str . ' ' . $year_str . ' ' . $month_str . ' group by id_user,year,month order by year,month';

        return $query;
    }

    /**
     * Retrieves the room data
     * @return array Array of objects containing the data from the database
     */
    function getData() {
        // Lets load the data if it doesn't already exist
        if (empty($this->_data)) {
            $query = $this->_buildQuery();
            $this->_data = $this->_getList($query);
        }

        return $this->_data;
    }
    
    function getListYear(){
        $id_user = JRequest::getVar('id_user');
        if ($id_user == null)
            $id_user = 0;
        
        if($id_user != 0){
            $query = 'select year from #__roombooking_costs where id_user = '.$id_user.' group by year order by year';
        }else $query = 'select year from #__roombooking_costs group by year order by year';
        
        return $this->_getList($query);
    }
    
    function getListMonth(){
        $id_user = JRequest::getVar('id_user');
        if ($id_user == null)
            $id_user = 0;
        $year = JRequest::getVar('year');
        if ($year == null)
            $year = 0;
        if($year == 0)return null;
        if($id_user != 0){
            $query = 'select month from #__roombooking_costs where id_user = '.$id_user.' group by month order by month';
        }else $query = 'select month from #__roombooking_costs group by month order by month';
        
        return $this->_getList($query);
    }

    /**
     * Method to get open rooms
     * @return object with data
     */
    function getRooms() {
        // Load the data
        $query = ' SELECT * FROM #__roombooking_rooms WHERE open = 1';
        $rooms = $this->_getList($query);

        return $rooms;
    }

    function getUserCharge($id_user) {
        $db = & JFactory::getDBO();
        // Load the data
        $query = ' SELECT charge FROM #__users WHERE id = ' . $id_user;
        $db->setQuery($query);
        $results = $db->loadObjectList();

        return $results[0]->charge;
    }

}
